//
//  YXTestViewController.m
//  YXImageBrowserDemo
//
//  Created by guo peng huang on 2021/5/23.
//

#import "YXTestViewController.h"
#import "YXImageBrowser.h"
#import "YXVideoData.h"
#import "YXUtilities.h"

@interface YXTestViewController ()<YXImageBrowserDataSource>

@end

@implementation YXTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    YXImageBrowser *browser = [[YXImageBrowser alloc] init];
    
    /// 关闭屏幕旋转
    browser.supportedOrientations = UIInterfaceOrientationMaskPortrait;
    
    /// 用代理处理数据源
    browser.dataSource = self;
    browser.currentPage = self.selectIndex;
    
    // 关闭入场和出场动效
    browser.defaultAnimatedTransition.showType = YXTransitionTypeNone;
    browser.defaultAnimatedTransition.hideType = YXTransitionTypeNone;
    // 删除工具视图（如需自定义 请自行实现）
    browser.toolViewHandlers = @[];
    [browser showToView:self.view containerSize:self.view.bounds.size];
}


#pragma mark - YXImageBrowserDataSource -
- (NSInteger)yx_numberOfCellsInImageBrowser:(YXImageBrowser *)imageBrowser {
    return self.dataArray.count;
}

- (id<YXDataProtocol>)yx_imageBrowser:(YXImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    NSString *obj = self.dataArray[index];
    
    if ([obj hasSuffix:@".mp4"] && [obj hasPrefix:@"http"]) {
        // 网络视频
        YXVideoData *data = [YXVideoData new];
        data.videoURL = [NSURL URLWithString:obj];
        data.interactionProfile.disable = YES;  //关闭手势交互
        data.shouldHideForkButton = YES;    //隐藏播放时的取消按钮
        data.singleTouchBlock = ^(YXVideoData * _Nonnull videoData) {};   //拦截单击事件
        return data;
     
    } else if ([obj hasSuffix:@".mp4"]) {
        
        // 本地视频
        NSString *path = [[NSBundle mainBundle] pathForResource:obj.stringByDeletingPathExtension ofType:obj.pathExtension];
        YXVideoData *data = [YXVideoData new];
        data.videoURL = [NSURL fileURLWithPath:path];
        data.interactionProfile.disable = YES;  //关闭手势交互
        data.shouldHideForkButton = YES;    //隐藏播放时的取消按钮
        data.singleTouchBlock = ^(YXVideoData * _Nonnull videoData) {};   //拦截单击事件
        return data;
        
    } else if ([obj hasPrefix:@"http"]) {
        
        // 网络图片
        YXImageData *data = [YXImageData new];
        data.imageURL = [NSURL URLWithString:obj];
        data.interactionProfile.disable = YES;  //关闭手势交互
        data.singleTouchBlock = ^(YXImageData * _Nonnull imageData) {};   //拦截单击事件
        return data;
        
    } else {
        
        // 本地图片
        YXImageData *data = [YXImageData new];
        data.imageName = obj;
        data.interactionProfile.disable = YES;  //关闭手势交互
        data.singleTouchBlock = ^(YXImageData * _Nonnull imageData) {};   //拦截单击事件
        return data;
        
    }
    
    return nil;
}

@end
